/**
 * 载入项目引导文件
 */
require('./bootstrap');

let box;

$(function () {
    let tableOptions = utils.bootstraptableOptions;
    //请求后台的URL
    tableOptions.url = page_params.url_list;
    //搜索参数
    tableOptions.queryParams = function (params) {//传递参数（*）
        //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        var temp = {
            //limit: params.limit,   //页面大小
            //offset: params.offset,
            pageSize:params.pageSize,
            page:params.pageNumber,
            keyword:$("#keyword").val(),
        };
        return temp;
    };
    tableOptions.columns = [
        {field: 'group_id',title: '#'},
        {field: 'group_key',title: '分组键名'},
        {field: 'group_name',title: '分组名'},
        {field: 'description',title: '描述'},
        {field: 'is_forever',title: '二维码类别', formatter: function (value, row, index) {
            if (value == 1){
                return '<span class="label label-primary">永久二维码</span>';
            } else{
                return '<span class="label label-primary">临时二维码</span>';
            }
        }},
        {
            field: 'operate',
            title: '操作',
            formatter: function (value, row, index) {
                let buttons = [];
                if (page_params.url_edit){
                    buttons.push('<button class="btn btn-primary btn-edit"><i class="fa fa-pencil"></i> 编辑</button>');
                }
                if (page_params.url_del){
                    buttons.push('<button class="btn btn-warning btn-del"><i class="fa fa-trash"></i> 删除</button>');
                }
                return buttons.join("\r\n");
            },//自定义方法，添加操作按钮
            events : {
                'click .btn-edit':function (e, value, row, index) {
                    $("#box-form form").resetForm();
                    $("#box-form form").attr('action', page_params.url_edit);
                    $("#box-form input[name='group_id']").val(row.group_id);
                    $("#box-form input[name='group_key']").val(row.group_key);
                    $("#box-form input[name='group_name']").val(row.group_name);
                    $("#box-form textarea[name='description']").val(row.description);
                    $("#box-form input[name='is_forever'][value="+ row.is_forever +"]").prop('checked', 'checked');
                    box = layer.open({
                        type: 1,
                        title: '编辑分组',
                        shadeClose: true,
                        anim: 2,
                        skin: 'layui-layer-molv', //加上边框
                        area: ['550px', '510px'], //宽高
                        content: $('#box-form')
                    });
                    return false;
                },
                'click .btn-del': function (e, value, row, index) {
                    utils.confirm(page_params.url_del,{'group_id':row.group_id}, (response, data) =>{
                        layer.close(box);
                        table.bootstrapTable('refresh');
                    });
                }
            }
        },
    ];
    let table = $("#table").bootstrapTable(tableOptions);
    //搜索按钮
    $("#btn-search").on('click', function () {
        table.bootstrapTable('refresh');
    });
    //添加
    $("#btn-add").on('click', function () {
        $("#box-form form").resetForm();
        $("#box-form form").attr('action', page_params.url_add);
        $("#box-form input[name='group_id']").val(0);
        $("#box-form input[name='is_forever'][value=1]").prop('checked', 'checked');
        box = layer.open({
            type: 1,
            title: '新增分组',
            shadeClose: true,
            anim: 2,
            skin: 'layui-layer-molv', //加上边框
            area: ['550px', '510px'], //宽高
            content: $('#box-form')
        });
    });
    //提交
    $('#btn-sub').on('click', function () {
        utils.ajaxSubmitForm($("#box-form form"), '', {}, (response, postdata) =>{
            data = response.data;
            utils.showToastr(data);
            if (data.status == 'success'){
                layer.close(box);
                table.bootstrapTable('refresh');
            }
        });
    });
});